package com.java.huxb.backend.user.controller;

import com.java.huxb.backend.user.controller.Vo.LoginReqVo;
import com.java.huxb.backend.user.service.UserServiceInterface;
import com.java.huxb.backend.utils.exception.CommonServiceException;
import com.java.huxb.backend.utils.util.JwtTokenUtil;
import com.java.huxb.backend.utils.util.MD5Util;
import com.java.huxb.backend.utils.vo.BaseResponseVO;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户模块 Controller
 */
@RestController
@RequestMapping("/user")
public class LoginController {

    @Resource
    private UserServiceInterface userService;
    
    /**
     * 用户登录
     * @return
     */
    @RequestMapping("/login")
    public BaseResponseVO login(LoginReqVo loginReqVo) throws CommonServiceException {

        String userId = userService.findUserByNameAndPwd(loginReqVo.getUserName(), loginReqVo.getUserPwd());

        JwtTokenUtil jwtTokenUtil = new JwtTokenUtil();
        String randomKey = jwtTokenUtil.getRandomKey();
        String token = jwtTokenUtil.generateToken(userId, randomKey);

        Map<String, Object> result = new HashMap<>();
        result.put("randomKey", randomKey);
        result.put("token", token);
        return BaseResponseVO.success(result);
    }


}
